package net.wanho.shop.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.po.Role;
import net.wanho.shop.service.RoleService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.PermVo;
import net.wanho.shop.vo.RoleAddVo;
import net.wanho.shop.vo.RoleFatherVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "06.角色表接口")
@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("所有角色列表")
    @GetMapping
    public AjaxResult findAll() {
        List<Role> roleList = roleService.findAll();
        return AjaxResult.success(roleList);
    }

    @ApiOperation("根据主键查询角色表")
    @GetMapping("/{roleId}")
    public AjaxResult findById(@PathVariable Serializable roleId) {
        return AjaxResult.success(roleService.findById(roleId));
    }

    @ApiOperation("新增角色表")
    @PostMapping
    public AjaxResult add(@RequestBody RoleAddVo role) {
        roleService.addRole(role);
        return AjaxResult.success();
    }

    @ApiOperation("修改角色表")
    @PutMapping("/{roleId}")
    public AjaxResult modify(@RequestBody Role role, @PathVariable Integer roleId) {
        roleService.modifyByRoleId(role, roleId);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除角色表")
    @DeleteMapping("/{roleId}")
    public AjaxResult removeById(@PathVariable Serializable roleId) {
        roleService.removeById(roleId);
        return AjaxResult.success();
    }

    @ApiOperation("角色权限列表")
    @GetMapping("/right")
    public AjaxResult Roles() {
        List<RoleFatherVo> roles = roleService.findRoles();
        return AjaxResult.success(roles);
    }

    @ApiOperation("删除角色指定权限")
    @DeleteMapping("/{roleId}/rights/{rightId}")
    public AjaxResult removePermissionsByRoleId(@PathVariable Integer rightId, @PathVariable Integer roleId) {
        List<PermVo> permVos = roleService.removePermissionsByRoleId(rightId, roleId);
        return AjaxResult.success(permVos);
    }

    @ApiOperation("角色授权")
    @PostMapping("{roleId}/rights")
    public AjaxResult addPermissionsByRoleId(@PathVariable Integer roleId, @RequestBody Map<String, String> ids) {
        roleService.addPermissionsByRoleId(roleId, ids);
        return AjaxResult.success();
    }
}
